package com.qianchang.microfinance.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.Button;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.UserLoginInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.dm.library.adapter.TextWatcherAdapter;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.PackageUtils;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.ContainsEmojiEditText;
import com.dm.library.widgets.element.DTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 意见反馈
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/30 16:28
 */


public class FeedbackActivity extends BaseActivity {

    private static final String TAG = "FeedbackActivity";
    @BindView(R.id.et_suggestion)
    ContainsEmojiEditText etSuggestion;
    @BindView(R.id.tv_input_number)
    DTextView tvInputNumber;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private int textLength = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initListener() {
        etSuggestion.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                textLength = s.length();
                tvInputNumber.setTextContent(String.format(getString(R.string.label_input_count), textLength));
                if (textLength > 0) {
                    btnCommit.setEnabled(true);
                } else {
                    btnCommit.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_feed_back));
        tvInputNumber.setTextContent(String.format(getString(R.string.label_input_count), textLength));
    }

    @OnClick(R.id.btn_commit)
    public void onViewClicked() {
        commit();
    }

    private void commit() {
        String content = etSuggestion.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.getInstant().show(this, "意见内容不能为空");
            return;
        }
        show();
        HttpParams params = new HttpParams();
        params.put("opinionContext", content);
        params.put("clientType", "1");      // 0：ios 1:adfroid 2 微信 3：pc
        params.put("versionNumbe", PackageUtils.getVersion(this));

        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.FEED_BACK, params, new RequestCallBack<UserLoginInfo>() {//UserLoginInfo
            @Override
            public void onSuccess(JsonResult<UserLoginInfo> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    ToastUtil.getInstant().show(FeedbackActivity.this, "谢谢您，您的宝贵意见是我们前进的动力！");
                    FeedbackActivity.this.finish();
                } else {
                    ToastUtil.getInstant().show(FeedbackActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(FeedbackActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(FeedbackActivity.this, msg);
            }
        });
    }

}
